--
-- Constant: Blood Elf Tower
--
CattleProd_EYE_OF_THE_STORM_BLOOD_ELF_TOWER = 1;

--
-- Constant: Draenei Ruins
--
CattleProd_EYE_OF_THE_STORM_DRAENEI_RUINS = 2;

--
-- Constant: Eye Of The Storm Fel Reaver Ruins
--
CattleProd_EYE_OF_THE_STORM_FEL_REAVER_RUINS = 3;

--
-- Constant: Eye Of The Storm Mage Tower
--
CattleProd_EYE_OF_THE_STORM_MAGE_TOWER = 4;

--
-- Constant: Eye Of The Storm Flag Spawn
--
CattleProd_EYE_OF_THE_STORM_FLAG_SPAWN = 5;

--
-- Constant: Eye Of The Storm Scoreboard Column Flags Captured
--
CattleProd_EYE_OF_THE_STORM_FLAGS_CAPTURED = 1;

--
-- Eye Of The Storm Scoreboard Columns
--
CattleProd_EYE_OF_THE_STORM_SCOREBOARD_COLUMNS = {
	CattleProd_EYE_OF_THE_STORM_FLAGS_CAPTURED
};

--
-- Setup Eye Of The Storm
--
function CattleProd_EyeOfTheStormSetup()
	CattleProd_Report(CattleProd_REPORT_LEVEL_MEDIUM,
		"Setup: %s", CattleProd_LocaleString(CattleProd_ZONE_EYE_OF_THE_STORM));

	-- player scoreboard function
	CattleProd_GetBattlegroundScoreboardMap_Function = CattleProd_GetEyeOfTheStormScoreboardMap;

	-- get defensible positions map
	CattleProd_GetBattlegroundDefensiblePositionsMap_Function = CattleProd_GetEyeOfTheStormDefensiblePositionsMap;

	-- get battleground player scoreboard effort
	CattleProd_CalculateBattlegroundScoreboardEffort_Function = CattleProd_CalculateEyeOfTheStormScoreboardEffort;

	-- get battleground defensive range
	CattleProd_GetBattlegroundDefensiveRange_Function = CattleProd_GetEyeOfTheStormDefensiveRange;

	-- calculate battleground defender effort
	CattleProd_CalculateBattlegroundDefenderEffort_Function = CattleProd_CalculateEyeOfTheStormDefenderEffort;
end

--
-- Get Eye Of The Storm scoreboard map.
--
-- playerIndex Player Index
--
-- return Player Scoreboard Map
--
function CattleProd_GetEyeOfTheStormScoreboardMap(playerIndex)
	local scoreboard = {};
	
	for i = 1, # CattleProd_EYE_OF_THE_STORM_SCOREBOARD_COLUMNS
	do
		local slotIndex = CattleProd_EYE_OF_THE_STORM_SCOREBOARD_COLUMNS[i];
		local count = GetBattlefieldStatData(playerIndex, slotIndex);

		scoreboard[slotIndex] = count;
	end
	
	return scoreboard;
end

--
-- Calculate Eye Of The Storm Scoreboard Effort.
--
-- scoreboardDelta Scoreboard Delta
--
-- return Effort
--
function CattleProd_CalculateEyeOfTheStormScoreboardEffort(scoreboardDelta)
	if (scoreboardDelta == nil)
	then
		CattleProd_Error("scoreboardDelta == nil");
		return nil;
	end
	if (type(scoreboardDelta) ~= "table")
	then
		CattleProd_Error("type(scoreboardDelta) == " .. type(scoreboardDelta));
		return nil;
	end

	local effortTotal = 0;

	-- falgs captured
	local flagsCaptured = scoreboardDelta[CattleProd_EYE_OF_THE_STORM_FLAGS_CAPTURED];
	if (flagsCaptured == nil)
	then
		CattleProd_Error("flagsCaptured == nil");
	else
		local bonusEffort = CattleProd_GetSetting(
			CattleProd_EYE_OF_THE_STORM_FLAGS_CAPTURED_BONUS_EFFORT);

		local effort = flagsCaptured * bonusEffort;

		effortTotal = effortTotal + effort;
	end

	-- effort total
	return effortTotal;
end

--
-- Eye Of The Storm Defensible Positions
--
local CattleProd_EyeOfTheStormDefensiblePositions = {
    CattleProd_EYE_OF_THE_STORM_BLOOD_ELF_TOWER,
    CattleProd_EYE_OF_THE_STORM_DRAENEI_RUINS,
    CattleProd_EYE_OF_THE_STORM_FEL_REAVER_RUINS,
    CattleProd_EYE_OF_THE_STORM_MAGE_TOWER,
    CattleProd_EYE_OF_THE_STORM_FLAG_SPAWN
}

--
-- Eye Of The Storm Defendable Locations
--
local CattleProd_EyeOfTheStormLocations = {
    [CattleProd_EYE_OF_THE_STORM_BLOOD_ELF_TOWER] =  { 55.7, 57.4 },
    [CattleProd_EYE_OF_THE_STORM_DRAENEI_RUINS] =    { 55.0, 42.0 },
    [CattleProd_EYE_OF_THE_STORM_FEL_REAVER_RUINS] = { 40.3, 57.0 },
    [CattleProd_EYE_OF_THE_STORM_MAGE_TOWER] =       { 40.9, 41.8 },
    [CattleProd_EYE_OF_THE_STORM_FLAG_SPAWN] =       { 48.0, 49.2, 48.6, 51.3, 47.8, 47.4 }
}

--
-- Get map of defensible positions.
--
-- return Defensible Position map
--
-- map: Position => { X, Y }
--
function CattleProd_GetEyeOfTheStormDefensiblePositionsMap()
	local defensiblePositions = {};

	local defensibleStates = {};
	
	local faction = CattleProd_GetFaction();
	if (faction == 0)
	then
		table.insert(defensibleStates, 9); -- GM - a
	elseif (faction == 1)
	then
		table.insert(defensibleStates, 10); -- GM - a
	end
	
	for i, landmark in pairs(CattleProd_EyeOfTheStormDefensiblePositions)
	do
		-- lookup location
		local name, description, textureIndex, x, y
		name, description, textureIndex, x, y = GetMapLandmarkInfo(landmark);
		
		-- nodes
		if (CattleProd_Contains(defensibleStates, textureIndex))
		then
			local locations = CattleProd_EyeOfTheStormLocations[landmark];
			if (locations == nil)
			then
				CattleProd_Error("landmark: " .. tostring(landmark) .. " locations: nil");
			else
				-- add locations to defensible positions
				table.insert(defensiblePositions, locations);
			end
		end
	end

	-- flag position
	local locations = CattleProd_EyeOfTheStormLocations[CattleProd_EYE_OF_THE_STORM_FLAG_SPAWN];
	table.insert(defensiblePositions, locations);
	
	-- TODO: add new moving flag position
	
	return defensiblePositions;
end

--
-- Get Eye Of The Storm Defensive Range.
--
-- return Defensive range
--
function CattleProd_GetEyeOfTheStormDefensiveRange()
	return 2.75;
end


--
-- Calculate Eye Of The Storm defender bonus effort.
--
-- defenderCount Defender Count
--
-- return Effort
--
function CattleProd_CalculateEyeOfTheStormDefenderEffort(defenderCount)
	if (defenderCount == nil)
	then
		CattleProd_Error("defenderCount == nil");
		return 0;
	end
	
	local defenderBonusEffort = CattleProd_GetSetting(CattleProd_EYE_OF_THE_STORM_DEFENDER_BONUS_EFFORT);
	if (defenderBonusEffort == nil)
	then
		CattleProd_Error("defenderBonusEffort == nil");
		return 0;
	end

	return defenderCount * defenderBonusEffort;
end
